package io.renren.common.utils;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import io.renren.modules.area_region.entity.Trans;

/**
 * 
 * 类描述：PHP序列化和反序列化工具
 * 
 * @author linmengmeng 创建时间：2019年1月15日 下午4:06:58
 */
public class SerializeUtil {

	/**
	 * 字符串序列化
	 * 
	 * @Title: serialize
	 * @Description: 字符串序列化
	 * @param cartIds
	 * @throws @return
	 *             String
	 */
	@SuppressWarnings("finally")
	public static String serialize(String[] cartIds) {
		String str = "";
		Map<String, Object> mapImage = new HashMap<String, Object>();
		Map<Integer, String> fileNameList = new HashMap<Integer, String>();
		try {
			for (int i = 0; i < cartIds.length; i++) {
				fileNameList.put(i + 1, cartIds[i]);
			}
			mapImage.put("buyer", fileNameList);
			byte[] a = PHPSerializer.serialize(mapImage);
			str = new String(a, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} finally {
			return str;
		}
	}
	@SuppressWarnings("finally")
	public static String serialize2(Map<String, Object> mapImage) {
		String str = "";
		try {
			byte[] a = PHPSerializer.serialize(mapImage);
			str = new String(a, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} finally {
			return str;
		}
	}
	@SuppressWarnings("finally")
	public static String serialize3(List<Map<String, Object>> mapImage) {
		String str = "";
		try {
			byte[] a = PHPSerializer.serialize(mapImage);
			str = new String(a, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} finally {
			return str;
		}
	}
	/**
	 * 将字符串反序列
	 * 
	 * @Title: unserialize
	 * @Description: 将字符串反序列
	 * @param content
	 * @return
	 * @throws @return
	 *             String
	 */
	@SuppressWarnings({ "finally", "unchecked" })
	public static String unserialize(String content) {
		String string = null;
		if (StringUtils.isEmpty(content)) {
			return string;
		}
		try {
			HashMap<String, Map<Integer, String>> hashMap = (HashMap<String, Map<Integer, String>>) PHPSerializer
					.unserialize(content.getBytes());
			System.out.println(hashMap.toString());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} finally {
			return string;
		}
	}

	/**
	 * 转换bean为map
	 *
	 * @param source
	 *            要转换的bean
	 * @param <T>
	 *            bean类型
	 * @return 转换结果
	 */
	public static <T> Map<String, Object> bean2Map(T source)  {
		Map<String, Object> result = new HashMap<>();

		Class<?> sourceClass = source.getClass();
		// 拿到所有的字段,不包括继承的字段
		Field[] sourceFiled = sourceClass.getDeclaredFields();
		for (Field field : sourceFiled) {
			field.setAccessible(true);// 设置可访问,不然拿不到private
			// 配置了注解的话则使用注解名称,作为header字段
			FieldName fieldName = field.getAnnotation(FieldName.class);
			if (fieldName == null) {
				try {
					result.put(field.getName(), field.get(source));
				} catch (IllegalArgumentException | IllegalAccessException e) {
					e.printStackTrace();
				}
			} else {
				if (fieldName.Ignore())
					continue;
				try {
					result.put(fieldName.value(), field.get(source));
				} catch (IllegalArgumentException | IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	/**
	 * map转bean
	 * 
	 * @param source
	 *            map属性
	 * @param instance
	 *            要转换成的备案
	 * @return 该bean
	 */
	public static <T> T map2Bean(Map<String, Object> source, Class<T> instance) {
		try {
			T object = instance.newInstance();
			Field[] fields = object.getClass().getDeclaredFields();
			for (Field field : fields) {
				field.setAccessible(true);
				FieldName fieldName = field.getAnnotation(FieldName.class);
				if (fieldName != null) {
					field.set(object, source.get(fieldName.value()));
				} else {
					field.set(object, source.get(field.getName()));
				}
			}
			return object;
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		String content="a:10:{s:12:\"start_weight\";s:1:\"1\";s:10:\"top_weight\";s:4:\"2000\";s:11:\"base_weight\";s:3:\"500\";s:8:\"base_fee\";s:3:\"124\";s:11:\"step_weight\";s:3:\"500\";s:8:\"step_fee\";s:2:\"33\";s:4:\"time\";s:15:\"3-5个工作日\";s:11:\"declaration\";s:2:\"20\";s:7:\"volueme\";s:1:\"1\";s:8:\"discount\";s:0:\"\";}";
		Trans trans = Trans.getUnserialize(content);
		Map<String, Object>map=bean2Map(trans);
		//System.out.println("map.toString()"+map.toString());
		String serialize2 = serialize2(map);
		System.out.println("serialize2---:  "+serialize2);
		System.out.println(content);
	}
}
